<template>
  <div>
    <img src="../assets/logo.png"/>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item label="账号" prop="username">
        <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
      </el-form-item>
      <el-form-item  label="密码" prop="pwd">
        <el-input type="password" placeholder="请输入密码" v-model="form.pwd"/>
      </el-form-item>
      <el-form-item prop="ptype">
        <el-radio v-model="form.ptype" label="0">管理员</el-radio>
        <el-radio v-model="form.ptype" label="1">学生</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
      title="温馨提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>请输入账号和密码</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { login } from "@/api/login.js";
import conf from "@/utils/config.js";
const { token } = conf;


export default {
  name: "Login",
  data() {
    return {
      radio: '1',
      form: {
        username: '',
        pwd: '',
        ptype: ''
      },
      //表单验证，需要再el-form-item 元素中增加prop属性
      rules: {
        username: [
          {required: true, message: '账号不能为空', trigger: 'blur'}
        ],
        pwd: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ]
      },
      //对话框显示和隐藏
      dialogVisible: false
    }
  },
  methods: {
    handleClose: function () { console.log("Handle Close，空函数"); },
    onSubmit(formName) {
      //为表单绑定验证功能
      this.$refs[formName].validate((valid) => {
        // console.log(valid);
        if (valid) {
// 验证通过 提交数据给后台 存token 路由跳转
// token 单纯存在sessionStorage、localStorage、cookie是可行的  若存在vuex中 刷新页面就没了
login(this.form)
  .then((res) => {
     console.log(res);

    //返回值格式 {"age":1,"id":1,"name":"zxc"}  注意 不是{fname:"John",lname:"Doe",age:25}这种格式
    var jsonObj = eval('('+res.request.response +')');
    console.log(jsonObj.code);
    // 存token setItem(key,value)
    if (jsonObj.code == 200) {
      //sessionStorage.setItem(token, res.data.token);
      console.log(this.form.ptype);
      if(this.form.ptype == '0')
      {
        this.$router.push("/admain/"+this.form.username);
      }
      else if(this.form.ptype == '1'){
        this.$router.push("/main/"+this.form.username);
      }
    } else if(jsonObj.code == 400){
       console.log(res.statusText);
      // statusText是后台写好的提示
      this.$notify.error({
        title: "错误",
        message: res.statusText,
      });
    }
  })
  .catch((err) => {
    this.$notify.error({
      title: "错误",
      message: "服务器内部错误",
    });
  });

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
  mounted() {
    document.querySelector('body').setAttribute('style', 'background-color:#90d7ec')
  },
  beforeDestroy() {
    document.querySelector('body').removeAttribute('style')
  },
}
</script>
<style lang="scss" scoped>
.login-box {
  border: 1px solid #DCDFE6;
  width: 350px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}
.login-title {
  font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑 Light",Arial,sans-serif;
  text-align: center;
  font-size: 30px ;
  margin: 0 auto 40px auto;
  color: #ffffff;
}
</style>
